package com.ssm.controller;

import com.ssm.pojo.House;
import com.ssm.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.PageData;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;
    @RequestMapping("/hotel_index")
    public String hotel_index(){ return "hotel_index"; }
    @RequestMapping("/main")
    public String main(){ return "main"; }
    //house_manager.jsp
    @RequestMapping("/house_manager")
    public String house_manager(ModelMap map){
        List<House> houseList=houseService.getAllHouses();
        map.addAttribute("houseList",houseList);
        return "house_manager";
    }
    @RequestMapping("/add")
    public void house_add(House house,HttpServletResponse resp){
        System.out.println("添加请求"+house);
        int n=houseService.insertHouse(house);
        System.out.println("添加了"+n+"条数据");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/update")
    public void house_update(House house,HttpServletResponse resp){
        System.out.println("添加请求"+house);
        int n=houseService.updateHouse(house);
        System.out.println("更新了"+n+"条数据");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/delete")
    public void house_delete(String name,HttpServletResponse resp){
        System.out.println("删除请求"+name);
        int n=houseService.deleteHouse(name);
        System.out.println("删除了"+n+"条数据");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }








//    暂停使用
//    @RequestMapping("/hotel_book")
//    public String hotel_book(){ return "hotel_book"; }
//    @RequestMapping("/house_add")
//    public String house_add(){ return "house_add"; }
//    @RequestMapping("/house_delete")
//    public String house_delete(){ return "house_delete"; }
//    @RequestMapping("/house_update")
//    public String house_update(){ return "house_update"; }
}
